import Model from './model'
import LafModel from '../laf/model.js'
import _ from 'lodash'
import assert from 'assert-plus'

const controller = {
	getTagsByLafId: async (id) => {
		return await Model.find({laf: id}).populate('tag').exec()
	},
	getLafsByTagId: async (id) => {
		return await Model.find({tag: id}).populate('laf').exec()
	},
	getLafListByTags: async (type, tags) => {
		const lafIds = await Model
													.find({tag: {$in: tags}, isDelete: false, createTime: {$gt: Date.now() - 2592000000}}) // 30天之内的
													.distinct('laf')
		return await LafModel.find({_id: {$in: lafIds}, isDelete: false, type: type}, '-__v -isDelete', {sort: {_id: -1}}).populate('tags')
	},
	// getMoreLafListByTags: async (type, lafId, tags, limit) => {
	// 	const lafIds = await Model
	// 												.find({tag: {$in: tags}, isDelete: false})
	// 												.populate({
	// 													path: 'laf', 
	// 													match: {type: type, _id: {$lt: lafId}},
	// 												  select: '-__v -isDelete',
	// 												  options: {sort: {_id: -1}}
	// 												})
	// 												.distinct('laf')
  //   return await LafModel.find({_id: {$in: lafIds}, isDelete: false}, '-__v -isDelete', {sort: {_id: -1}, limit: limit}).populate('tags')
	// },
	createByTagIds: async (lafId, tagIds) => {
		return await Model
									.create(tagIds.map((tagId) => {
										return {
											laf: lafId,
											tag: tagId
										}
									}))
	},
	deleteLad: async (id) => {
		return await Model.update({_id: id}, {$set: {isDelete: true}}).exec()
	}
};

export default controller;